package inf.coach.mobile;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

public class pois extends Activity  {

	public static long userid = 0;
	public static int range = 100;
	public static TextView info = null;
	public static EditText range_poi = null;
	public static ListView list = null;
	public static Button send=null;
	public static Button getDir=null;
	public static double lat = 0;
	public static double lon = 0;

	private static String title = "function page, Search pois around. ";
	public static int num = 0;
	public static int nextposition = 1;
	public static int lastposition = -1;
	public static long osmid = 0;
	public static String POIname = null;
	
	

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.pois);
		setTitle("POIs Seite, Anzeige des Suchergebnisses");

		//info = (TextView) findViewById(R.id.info_poi);

		Bundle bundle = this.getIntent().getExtras();
		userid = bundle.getLong("userid");
		lat=bundle.getDouble("lat");
		lon=bundle.getDouble("lon");
		int ran = bundle.getInt("dis");
		final boolean verkehr = bundle.getBoolean("verkehr");
		final boolean gesundheit = bundle.getBoolean("gesundheit");
		final boolean essen_und_trinken = bundle.getBoolean("essen_und_trinken");
		final boolean bank_und_post = bundle.getBoolean("bank_und_post");
		final boolean edu = bundle.getBoolean("sonstiges");
		
//		final List poi = new ArrayList();

		list = (ListView) findViewById(R.id.poilistview);	
		TextView results= (TextView)findViewById(R.id.textView_results);
		


		if (ran != 0) {
			System.out.println(ran);
			if (lat != 0 && lon != 0) {
				if(ConnectionReceiver.isInternetConnected(getApplicationContext()))
					{
						List<pois> newlistview=webservice.poiviaGeo(lat,lon, ran,true, true,verkehr,gesundheit,essen_und_trinken,bank_und_post,edu);
						/*List newlistview=webservice.poiviaGeo(lat,lon, ran,true, true,true,true,true,true,true);*/
						if (!newlistview.equals(null)) {
							listview(newlistview);
							results.setText("Insgesamt "+ num +" Ergebnisse gefunden!");
							}
						else{
							System.out.println("empty");
							message.show(pois.this,"Fehler", "Keine POI gefunden !");
						}
						}
						else{
							message.show(pois.this,"Fehler", "Keine Internetverbindung !");
							//mTts.speak("Information Window, Error, There is no Internet Connection , Ok button", TextToSpeech.QUEUE_FLUSH, null);
						}
					}
				}
		
/*		if (!poi.equals(null)) {
			listview(poi);
		}*/
		
		
	}
	
	public void listview(final List pois) {

		ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
		if (!pois.equals(null)) {
			num = pois.size();
			
			View headerView = getLayoutInflater().inflate(R.layout.listview,
					null);

			for (int i = 0; i < num; i++) {

				HashMap<String, String> map = new HashMap<String, String>();
				poi apoi = (poi) pois.get(i);
				if(apoi.amenity.equals("bus_stop"))
				{
					apoi.amenity="Haltestelle";
				}
				if(apoi.amenity.equals("crossing"))
				{
					apoi.amenity="Kreuzung";
				}
				if(apoi.amenity.equals("pharmacy"))
				{
					apoi.amenity="Apotheke";
				}
				if(apoi.amenity.equals("hospital"))
				{
					apoi.amenity="Krankenhaus";
				}
				if(apoi.amenity.equals("cafe"))
				{
					apoi.amenity="Café";
				}
				if(apoi.amenity.equals("fast_food"))
				{
					apoi.amenity="Fastfood";
				}
				if(apoi.amenity.equals("atm"))
				{
					apoi.amenity="Geldautomat";
				}
				if(apoi.amenity.equals("school"))
				{
					apoi.amenity="Schule";
				}
				if(apoi.amenity.equals("university"))
				{
					apoi.amenity="universität";
				}
				if(apoi.amenity.equals("library"))
				{
					apoi.amenity="Bibliothek";
				}
				map.put("Point of interest name",(i+1)+". " + apoi.name);
/*				map.put("A Building", "Amenity: "+apoi.amenity+" Building: " + apoi.building);*/
				map.put("Amenity", "Kategorie: "+apoi.type+", Typ: " + apoi.amenity);
				mylist.add(map);
			}

			SimpleAdapter mSchedule = new SimpleAdapter(this, mylist,// 数据来源
					R.layout.listview,// ListItem的XML实现

					// 动态数组与ListItem对应的子项
					new String[] { "Point of interest name", "Amenity" },

					// ListItem的XML文件里面的两个TextView ID

					new int[] { R.id.listview_title, R.id.listview_info });

			mSchedule.notifyDataSetChanged();
			// 添加并且显示

			list.setAdapter(mSchedule);

			// list.addHeaderView(headerView);

			list.setItemsCanFocus(true);

			list.setSelectionAfterHeaderView();

			list.setOnItemClickListener(new OnItemClickListener() {


				public void onItemClick(AdapterView<?> arg0, View arg1,
						int arg2, long arg3) {
					// TODO Auto-generated method stub
					Intent intent = new Intent();
					Bundle bundle = new Bundle();
					bundle.putLong("userid", userid);
					poi apoi = (poi) pois.get(arg2);
					bundle.putLong("osmid", apoi.osm_id);
					bundle.putString("name", apoi.name);
					
					if(apoi.amenity.equals("Haltestelle"))
					{
						
						intent.setClass(pois.this, Station.class);
//						intent.setClass(pois.this, Crossinganno.class);
						intent.putExtras(bundle);
						startActivity(intent);
					
					}
					else if(apoi.amenity.equals("Kreuzung"))
					{
						intent.setClass(pois.this, Crossinganno.class);
						intent.putExtras(bundle);
						startActivity(intent);
					}
					
					else
					{
						intent.setClass(pois.this, BuildingAnno.class);
						intent.putExtras(bundle);
						startActivity(intent);
					}

				}

			});

			
		}
	}
	
/*	 protected void dialog() { 
	        AlertDialog.Builder builder = new Builder(pois.this); 
	        builder.setMessage("There is no entrance annotation for this building, do you want to create ?"); 
	        builder.setTitle("Information"); 
	        builder.setPositiveButton("ok", 
	                new android.content.DialogInterface.OnClickListener() { 
	                    @Override 
	                    public void onClick(DialogInterface dialog, int which) { 
	                        dialog.dismiss(); 
	                        Intent intent = new Intent();
	    					Bundle bundle = new Bundle();
	    					bundle.putBoolean("flag", false);
	    					bundle.putLong("osmid", osmid);
	    					bundle.putLong("userid", userid);
	    					intent.setClass(pois.this, TrafficLightAnno.class);
	    					intent.putExtras(bundle);
	    					startActivity(intent);
	                    }

	                }); 
	        builder.setNegativeButton("cancel", 
	                new android.content.DialogInterface.OnClickListener() { 
	                    @Override 
	                    public void onClick(DialogInterface dialog, int which) { 
	                        dialog.dismiss(); 
	                    } 
	                }); 
	        builder.create().show(); 
	    }*/

    protected void dialoggoback() { 
        AlertDialog.Builder builder = new Builder(pois.this); 
        builder.setMessage("Möchten Sie zur letzten Seite zurück springen?"); 
        builder.setTitle("Information"); 
        builder.setPositiveButton("ok", 
                new android.content.DialogInterface.OnClickListener() { 
                    @Override 
                    public void onClick(DialogInterface dialog, int which) { 
                        dialog.dismiss(); 
                        pois.this.finish(); 
                    }

                }); 
        builder.setNegativeButton("abbrechen", 
                new android.content.DialogInterface.OnClickListener() { 
                    @Override 
                    public void onClick(DialogInterface dialog, int which) { 
                        dialog.dismiss(); 
                    } 
                }); 
        builder.create().show(); 
    } 
    @Override 
    public boolean onKeyDown(int keyCode, KeyEvent event) { 
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) { 
            dialoggoback(); 
            return false; 
        } 
        return false; 
    } 


}